package design.simdi.code.final_paper.site.panels.blog;

import org.apache.wicket.Component;
import org.apache.wicket.Page;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.markup.html.form.TextArea;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;
import org.apache.wicket.model.PropertyModel;

import design.simdi.code.final_paper.core.entities.Mesaj;
import design.simdi.code.final_paper.core.entities.Person;
import design.simdi.code.final_paper.core.entities.User;
import design.simdi.code.final_paper.core.enums.MessageStatus;
import design.simdi.code.final_paper.site.application.MyAuthenticatedWebSession;
import design.simdi.code.final_paper.site.pages.BasePage;
import design.simdi.code.final_paper.site.panels.YesNoPanel;

/**
 * 
 * @author Marius Dumitriu - (Jun 26, 2010 - 6:15:37 PM)
 * 
 */
public class TrimiteMesajPanel extends YesNoPanel {

	private static final long serialVersionUID = 1L;

	public TrimiteMesajPanel(String id, IModel<?> model, Component back) {
		super(id, model);
		this.backComponent = back;
		getBack().setDefaultFormProcessing(false);
	}

	private Person destinatar;
	private Component backComponent;
	private Mesaj mesaj;
	private TextField<String> dest;
	private TextField<String> exped;
	private TextField<String> subject;
	private TextArea<String> content;

	public void panelInit() {
		super.panelInit();
		destinatar = (Person) getDefaultModelObject();
		mesaj = new Mesaj();

		User currentUser = ((MyAuthenticatedWebSession) getSession()).getCurrentUser();

		Person currentPerson = getUserService().findPersonByUser(currentUser);

		mesaj.setExpeditor(currentPerson);
		mesaj.setDestinatar(destinatar);
		mesaj.setStatus(MessageStatus.NEW);

		getForm().add(dest = new TextField<String>("dest", new Model<String>(mesaj.getDestinatar().toString())));
		getForm().add(exped = new TextField<String>("exped", new Model<String>(mesaj.getExpeditor().toString())));
		getForm().add(subject = new TextField<String>("subject", new PropertyModel<String>(mesaj, "title")));
		getForm().add(content = new TextArea<String>("content", new PropertyModel<String>(mesaj, "content")));

		subject.setRequired(true);
		content.setRequired(false);

		subject.setType(String.class);
		content.setType(String.class);

		dest.setEnabled(false);
		exped.setEnabled(false);

	}

	@Override
	public void onYESButtonSubmit(AjaxRequestTarget target) {

		getApplicationService().saveMesaj(mesaj);

		Page page = this.getPage();

		page.get("content:tabs:panel").replaceWith(backComponent);

		target.addComponent(page.get("content"));

		target.addComponent(page.get(BasePage.ERRORS_PANEL));
		info(page.getString("operation.success"));
	}

	@Override
	public void onCANCELButtonSubmit(AjaxRequestTarget target) {
		Page page = this.getPage();

		page.get("content:tabs:panel").replaceWith(backComponent);

		target.addComponent(page.get("content"));

		target.addComponent(page.get(BasePage.ERRORS_PANEL));
		warn(page.getString("operation.canceled"));
	}

}
